<template>
	<view class="wrap">
		<view class="topBc"></view>
		<view class="content formWrap">
			<view class="title" style="margin-top: 0;">基本信息</view>
			
			<u-form :model="formData" ref="uForm" :rules="rules">
			    <u-form-item v-if="formKeys.ancheyear" label="报告年度" label-width="auto" borderBottom>
					<u-input v-model="formData.ancheyear" placeholder="报告年度" disabled disabledColor="rgba(0,0,0,0)" color="#E4B269" border="none" inputAlign="right"></u-input>
			    </u-form-item>
			    <u-form-item v-if="formKeys.name" label="个体名称" label-width="auto" borderBottom>
					<u-input v-model="formData.name" placeholder="个体名称" disabled disabledColor="rgba(0,0,0,0)" color="#E4B269" border="none" inputAlign="right"></u-input>
			    </u-form-item>
				<u-form-item v-if="formKeys.entname" label="企业名称" label-width="auto" borderBottom>
					<u-input v-model="formData.entname" placeholder="企业名称" disabled disabledColor="rgba(0,0,0,0)" color="#E4B269" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.dependententname" label="隶属企业名称" prop="dependententname" required label-width="auto" borderBottom>
					<u-input v-model="formData.dependententname" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.depuniscid" label="隶属企业统一社会信用代码/注册号" prop="depuniscid" required label-width="auto" borderBottom>
					<u-input v-model="formData.depuniscid" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.regno" label="统一社会信用代码/注册号" label-width="300rpx" borderBottom>
					<u-input v-model="formData.regno" placeholder="统一社会信用代码/注册号" disabled disabledColor="rgba(0,0,0,0)" color="#E4B269" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.traname" label="经营者姓名" label-width="auto" borderBottom>
					<u-input v-model="formData.traname" placeholder="经营者姓名" disabled disabledColor="rgba(0,0,0,0)" color="#E4B269" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.addr" label="通讯地址" prop="addr" required label-width="auto" borderBottom>
					<u-input v-model="formData.addr" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.postalcode" label="邮政编码" prop="postalcode" required label-width="auto" borderBottom>
					<u-input v-model="formData.postalcode" type="number" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.tel" label="联系电话" prop="tel" required label-width="auto" borderBottom>
					<u-input v-model="formData.tel" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.email" label="电子邮箱" prop="email" required label-width="auto" borderBottom>
					<u-input v-model="formData.email" placeholder="请输入" border="none" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.fundam" label="资金数额" prop="fundam" required label-width="auto" borderBottom>
					<u-input v-model="formData.fundam" type="number" placeholder="请输入" border="none" inputAlign="right">
						<view class="units_suffix" slot="suffix">
							<text><text class="units_suffix_colorRed">万</text>(元)</text>
						</view>					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.assgro" label="资金数额" prop="assgro" required label-width="auto" borderBottom>
					<u-input v-model="formData.assgro" type="number" placeholder="请输入" border="none" inputAlign="right">
						<view class="units_suffix" slot="suffix">
							<text><text class="units_suffix_colorRed">万</text>(元)</text>
						</view>					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.memnum" label="成员人数" prop="memnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.memnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.farnum" label="其中农民人数" prop="farnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.farnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.annnewmemnum" label="本年度新增" prop="annnewmemnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.annnewmemnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.annredmemnum" label="退出" prop="annredmemnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.annredmemnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<view class="textLength">
					<u-form-item v-if="formKeys.mainbusiact" label="主营业务活动" prop="mainbusiact" required label-width="auto" borderBottom>
						<u--textarea v-model="formData.mainbusiact" placeholder="请输入" border="none" inputAlign="right" autoHeight ></u--textarea>
					</u-form-item>
				</view>
				<u-form-item v-if="formKeys.empnum" label="从业人数" prop="empnum" required label-width="auto" :borderBottom="!formKeys.empnumdis">
					<u-input v-model="formData.empnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<view 
							slot="prefix" style="transform: translateX(-12rpx);"
							@click="$util.showModal({content:'在报告年度年末，在企业(机构) 及其分支机构工作并取得劳动报酬或者经营收入的全部人员数量，包含劳务派遣人员和临时工。'})"
						>
							<u-icon name="question-circle" :color="bgColor" size="34rpx"></u-icon>
						</view>
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.empnumdis" label="是否公示" prop="empnumdis" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.empnumdis" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.womempnum" label="其中女性从业人数" prop="womempnum" required label-width="auto">
					<u-input v-model="formData.womempnum" type="number" placeholder="请输入" border="none" inputAlign="right">
						<u--text
							text="人"
							slot="suffix"
						></u--text>
					</u-input>
				</u-form-item>
				<u-form-item v-if="formKeys.womempnumdis" label="是否公示" prop="womempnumdis" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.womempnumdis" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item v-if="formKeys.busst" label="企业经营状态" prop="busst" required label-width="auto" borderBottom @click="showSelectFun('busst')">
					<u--input v-model="formData.busstCn" placeholder="请选择" border="none" inputAlign="right" disabled disabledColor="rgba(0,0,0,0)"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item v-if="formKeys.holdingsmsg" label="企业控股情况" prop="holdingsmsg" required label-width="auto" @click="showSelectFun('holdingsmsg')">
					<u--input v-model="formData.holdingsmsgCn" placeholder="请选择" border="none" inputAlign="right" disabled disabledColor="rgba(0,0,0,0)"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item v-if="formKeys.holdingsmsgdis" label="是否公示" prop="holdingsmsgdis" required label-width="auto" borderBottom>
					<u-radio-group v-model="formData.holdingsmsgdis" placement="row">
						<u-radio v-for="item,index in stateRanges" :key="index" :label="item.text" :name="item.value" :activeColor="bgColor"></u-radio>
					</u-radio-group>
				</u-form-item>
				
				
			</u-form>
			
		</view>
		<view class="submitBtn_bc"></view>
		<view class="submitBtn_ubtn">
			<u-button :disabled="!$util.submitState(year,state)" @click="submit()">提交</u-button>
		</view>
		
		
		
		<!-- 企业经营状态 -->
		<u-action-sheet :show="busstShowSelect" :actions="busstTypeList" cancelText="取消" @close="hideSelectFun('busst')" @select="selectBtn($event,'busst')"></u-action-sheet>
		<!-- 企业控股情况 -->
		<u-action-sheet :show="holdingsmsgShowSelect" :actions="holdingsmsgTypeList" cancelText="取消" @close="hideSelectFun('holdingsmsg')" @select="selectBtn($event,'holdingsmsg')"></u-action-sheet>
		
	</view>
</template>

<script>
export default {
	data(){
		return{
			minFormName: 'baseinfo', //小表单类型名
			regnoInfo: this.$store.state.regnoInfo,
			year: '',
			state: '',
			bgColor: this.$config.bgColor,
			
			stateRanges: [
				{text: '是',value: '1'},
				{text: '否',value: '2'}
			],
			busstShowSelect: false,
			holdingsmsgShowSelect: false,
			busstTypeList: [],
			holdingsmsgTypeList: [],
			
			formData: {
				entname: '',
				dependententname: '',
				depuniscid: '',
				addr: '',
				postalcode: '',
				tel: '',
				email: '',
				fundam: '',
				assgro: '',
				memnum: '',
				farnum: '',
				annnewmemnum: '',
				annredmemnum: '',
				mainbusiact: '',
				empnum: '',
				empnumdis: '',
				womempnum: '',
				womempnumdis: '',
				busst: '',
				holdingsmsg: '',
				holdingsmsgdis: '',
				
				ancheyear: '', //固定值
				name: this.$store.state.regnoInfo?this.$store.state.regnoInfo.entname:'', //固定值
				uniscid: '', //固定值
				regno: this.$store.state.regnoInfo?this.$store.state.regnoInfo.regno:'', //固定值
				traname: this.$store.state.regnoInfo?this.$store.state.regnoInfo.lerep:'', //固定值
			},
			rules: {},
			rulesAll: {
				dependententname: this.$typeData.ruleData.inputRule,
				depuniscid: this.$typeData.ruleData.inputRule,
				addr: this.$typeData.ruleData.inputRule,
				postalcode: this.$typeData.ruleData.peopleRule,
				tel: this.$typeData.ruleData.telRule,
				email: this.$typeData.ruleData.emailRule,
				fundam: this.$typeData.ruleData.moneyRule,
				assgro: this.$typeData.ruleData.moneyRule,
				memnum: this.$typeData.ruleData.peopleRule,
				farnum: this.$typeData.ruleData.peopleRule,
				annnewmemnum: this.$typeData.ruleData.peopleRule,
				annredmemnum: this.$typeData.ruleData.peopleRule,
				mainbusiact: this.$typeData.ruleData.inputRule,
				empnum: this.$typeData.ruleData.peopleRule,
				empnumdis: this.$typeData.ruleData.disRule,
				womempnum: this.$typeData.ruleData.peopleRule,
				womempnumdis: this.$typeData.ruleData.disRule,
				busst: this.$typeData.ruleData.disRule,
				holdingsmsg: this.$typeData.ruleData.disRule,
				holdingsmsgdis: this.$typeData.ruleData.disRule,
			},
			formKeys: {},
		}
	},
	onLoad(option) {
		this.year = option.year;
		this.state = option.state;
		this.formData.ancheyear = option.year;
		this.busstTypeList = this.$util.getLocalDictMap('busst','id','name');
		this.holdingsmsgTypeList = this.$util.getLocalDictMap('holdingsmsg','id','name');
		this.getFormKey();
		this.getFormData();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules)
	},
	methods:{
		// 获取小表单数据
		getFormData(){
			let url = this.$typeData.minFormUrl.get(this.minFormName);
			if(!url){
				uni.showModal({
					title: '暂无接口'
				})
				return;
			}
			this.$http.sendRequest({
				url: url,
				data: {
					regno: this.regnoInfo.regno,
					ancheyear: this.year
				}
			})
			.then(res=>{
				console.log('接口返回',res.data);
				let data = res.data;
				if(data){
					let keys = Object.keys(this.formKeys);
					keys.forEach(item=>{
						this.formData[item] = data[item];
					})
				}				
				// 特殊处理，固定值
				this.formData.ancheyear = this.formData.ancheyear || this.year;
				this.formData.name = this.formData.name || this.regnoInfo.entname;
				this.formData.entname = this.formData.entname || this.regnoInfo.entname;
				this.formData.uniscid = this.formData.uniscid || this.regnoInfo.uniscid;
				this.formData.regno = this.formData.regno || this.regnoInfo.regno;
				this.formData.traname = this.formData.traname || this.regnoInfo.lerep;
				
				this.getEnttype();
			})
			.catch(err=>{console.log(33,err)})
		},
		//获取小表单拥有字段
		getFormKey(){
			let formKeys = this.$util.getFormKeys(this.minFormName,this.regnoInfo.enttype);
			this.formKeys = formKeys;
			let keys = Object.keys(formKeys);
			keys.forEach(item=>{
				this.rules[item] = this.rulesAll[item];
			})
		},
		getEnttype(){
			if(!this.formData.tel){
				this.formData.tel = this.regnoInfo.tel;
			}
		},
		submit(){
			let _this = this;
			this.$refs.uForm.validate().then(res => {
				this.saveBtn();
			}).catch(errors => {
				this.$util.showToast({title:'请完善信息'})
			})
		},
		saveBtn(){
			let data = {
				regno: this.regnoInfo.regno,
				ancheyear: this.year,
			}
			data = this.$util.setSaveFormData(this.formKeys,data,this.formData);
			this.$http.sendRequest({
				url: '/annual/report/baseinfo/update-baseinfo',
				method: 'PUT',
				data: data
			})
			.then(res=>{
				this.setRecord();
			})
			.catch(err=>{})
		},
		// 打开选择器
		showSelectFun(typeName){
			let name = `${typeName}ShowSelect`;
			this[name] = true;
		},
		// 关闭选择器
		hideSelectFun(typeName){
			let name = `${typeName}ShowSelect`;
			this[name] = false;
		},
		// 选择回调
		selectBtn(e,typeName){
			this.setSelectData(`${typeName}TypeList`,typeName,e.id);
			this.$refs.uForm.validateField(typeName);
		},
		// 选择后的文案、回填
		setSelectData(listName,dataName,id){
			let arr = this[listName];
			let obj = {};
			for(let i=0; i<arr.length; i++){
				if(arr[i].id == id){
					obj = arr[i];
					break;
				}
			}
			if(obj.id){
				this.formData[dataName] = obj.id;
				this.formData[`${dataName}Cn`] = obj.name;
			}
		},
		
		
		//存一下记录
		setRecord(){
			let fillingItem = this.$minFormType.get(this.minFormName);
			this.$http.sendRequest({
				url: '/annual/report/record',
				method: 'POST',
				data: {
					regno: this.regnoInfo.regno,
					year: this.year,
					fillingItem: fillingItem,
					isImp: '0',
				},
				hideLoad: true,
			})
			.then(res=>{
				uni.navigateBack();
			})
			.catch(err=>{})
		},
		goPage(path){
			uni.navigateTo({
				url: path
			})
		},
	}
}
</script>

<style lang="less" scoped>
.wrap{
	width: 100vw;
	min-height: 100vh;
	padding-top: 16rpx;
	box-sizing: border-box;
	position: relative;
	background-color: #FFF;
	
	.topBc{
		height: 10rpx;
		background-color: #F5F6F8;
	}
}

.content{
	padding: 30rpx 36rpx;
	background-color: #FFF;
	
	.title{
		color: #E4B269;
		font-size: 36rpx;
		font-weight: bold;
		border-left: 8rpx solid #E4B269;
		line-height: 40rpx;
		padding-left: 16rpx;
		margin-bottom: 30rpx;
		margin-top: 30rpx;
	}
	
	
}
</style>